package com.sskj.gcc.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.hey.lib.HeySpinner;
import com.sskj.gcc.R;
import com.sskj.gcc.presenter.ChargeMoneyActivityPresenter;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.ChargeCoinItemBean;
import com.sskj.gocoin.m.bean.RechargeInfoBean;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.CopyUtil;
import com.sskj.gocoin.utils.ImgUtils;
import com.sskj.gocoin.utils.QRCodeUtil;
import com.sskj.gocoin.v.activity.CapitalDetailsActivity;
import com.sskj.gocoin.v.activity.RechargeRecordActivity;
import com.sskj.gocoin.v.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

public class ChargeMoneyActivity extends BaseActivity<ChargeMoneyActivityPresenter> {
    @BindView(R.id.toolBar)
    ToolBarLayout toolBar;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;
    @BindView(R.id.tv_save_qr)
    TextView tvSaveQr;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.spinner)
    HeySpinner spinner;
    @BindView(R.id.recharge_hint)
    TextView rechargeHint;
    private List<ChargeCoinItemBean> assetBeans;
    private String assetName;
    private int assetId;
    private Bitmap mbitmap;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recharge2;
    }

    @Override
    public ChargeMoneyActivityPresenter getPresenter() {
        return new ChargeMoneyActivityPresenter();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ChargeMoneyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        toolBar.setLeftButtonOnClickLinster(view -> finish());
        toolBar.setRightButtonOnClickLinster(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mActivity, CapitalDetailsActivity.class));
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter.requestUserAssetList();
    }

    public void onDataSuccess(List<ChargeCoinItemBean> data) {
        Flowable.fromIterable(data)
                .filter(aseetBean -> !aseetBean.getCode().equals("GCT"))
                .toList()
                .subscribe(aseetBeans -> {
                    assetBeans = aseetBeans;
                    initSpinner();
                });
    }

    private void initSpinner() {
        List<String> data = new ArrayList<>();
        for (ChargeCoinItemBean bean : assetBeans) {
            data.add(bean.getCode());
        }
        spinner.attachData(data);
        spinner.setOnSelectListener(position -> {
            assetName = assetBeans.get(position).getCode();
            assetId = assetBeans.get(position).getId();
            mPresenter.recharge(assetId);
            changeContent();
        });
        spinner.setSelectIndex(0);

        //0 usdt  else 其他币种
        mPresenter.recharge(1);
        changeContent();
    }

    private void changeContent() {
        tv1.setText(String.format(App.INSTANCE.getString(R.string.appChargeMoney2), assetName));
    }

    public void updateView(RechargeInfoBean bean) {
        if (bean==null){
            return;
        }
        tvCode.setText(bean.getAddress());

        RequestOptions options = new RequestOptions();
        options.skipMemoryCache(true);
        options.placeholder(com.sskj.gocoin.R.mipmap.app_logo);
        Glide.with(this).load(bean.getQrcode()).apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(ivQrCode);

//        QRCodeUtil.createImage(bean.getQrcode(), (int) RudenessScreenHelper.pt2px(this, 230), new QRCodeUtil.OnEncodeQRCodeCallback() {
//            @Override
//            public void onAnalyzeSuccess(Bitmap bitmap) {
//                mbitmap = bitmap;
//                ivQrCode.setImageBitmap(bitmap);
//            }
//
//            @Override
//            public void onAnalyzeFailed() {
//
//            }
//        });
        ClickUtil.click(tvCopy, () -> {
            CopyUtil.copy(tvCode.getText().toString());
        });
        ClickUtil.click(tvSaveQr, () -> {
            ImgUtils.saveImageToGallery(ChargeMoneyActivity.this, ivQrCode);
        });

    }

}
